package com.chenxin.wms.process.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.LocationEntity;
import com.chenxin.wms.domain.TaskEntity;
import com.chenxin.wms.mapper.LocationMapper;
import com.chenxin.wms.mapper.TaskMapper;
import com.chenxin.wms.process.common.ISystemProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ISystemProcessImpl implements ISystemProcess {
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public ProcResult procAutoLocationUp(String contNo, String locateNo, Long creator) {
        // 参数校验
        if (StringUtils.isBlank(contNo)) {
            return ProcResult.error("auto_up_err#001", "LPN不能为空");
        }
        if (StringUtils.isBlank(locateNo)) {
            return ProcResult.error("auto_up_err#002", "货位号不能为空");
        }
        // 业务校验
        QueryWrapper<LocationEntity> locateNoQuery = new QueryWrapper<>();
        locateNoQuery.eq("location_no", locateNo)
                .eq("location_status", "0").last("limit 1");
        LocationEntity locationEntity = locationMapper.selectOne(locateNoQuery);
        if (locationEntity == null) {
            return ProcResult.error("auto_up_err#003", "货位【" + locateNo + "】未在【wms_location】表中维护!");
        }
        // 货位是否可用
        UpdateWrapper<LocationEntity> locationUw = new UpdateWrapper<>();
        locationUw.lambda().eq(LocationEntity::getLocationNo, locateNo)
                .set(LocationEntity::getLocationStatus, "2")
                .set(LocationEntity::getUpdateDate,new Date())
                .setSql("");
        int rows = locationMapper.update(locationUw);

        // 相关上架任务是否存在（是否存在多场景业务？）
        QueryWrapper<TaskEntity> taskQw = new QueryWrapper<>();
        taskQw.lambda().eq(TaskEntity::getFromContNo, contNo)
                .le(TaskEntity::getTaskStatus, "2")
                .groupBy(TaskEntity::getTaskSubType).groupBy(TaskEntity::getToPosition);
        TaskEntity taskEntity = taskMapper.selectOne(taskQw);
        if (taskEntity == null) {
            return ProcResult.error("auto_up_err#004", "LPN【" + contNo + "】不存在可用的上架任务!");
        }
        return ProcResult.success("LPN【" + contNo + "】成功上架至立库货位【" + locateNo + "】");
    }

    @Override
    public ProcResult procAutoLocationDown(String contNo, String locateNo, Long creator) {
        return ProcResult.success("LPN【" + contNo + "】从货位【" + locateNo + "】下架成功");
    }

    @Override
    public ProcResult procManualLocationUp(String contNo, String locateNo, Long creator) {
        return ProcResult.success("LPN【" + contNo + "】成功上架至平库货位【" + locateNo + "】");
    }

    @Override
    public ProcResult procManualLocationDown(String contNo, String locateNo, Long creator) {
        return ProcResult.success("LPN【" + contNo + "】从货位【" + locateNo + "】");
    }
}
